﻿package 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BitmapDataChannel;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.text.AntiAliasType;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	/**
	 * @author Nicolas Barradeau
	 * http://en.nicoptere.net
	 */
	public class BitmapMask extends Sprite 
	{
		
		private var bmp:Bitmap;
		private var bd:BitmapData;
		
		private var tf:TextFormat;
		private var txt:TextField;
		
		public function BitmapMask():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			txt = new TextField();
			txt.autoSize = TextFieldAutoSize.LEFT;
			txt.antiAliasType = AntiAliasType.ADVANCED;
			txt.text = 'ABCDEF';
			
			tf = new TextFormat( 'Verdana', 48, 0xFFFFFF );
			txt.setTextFormat( tf );
			
			
			bd = new BitmapData( txt.width, txt.height, true, 0x000000 );
			bd.perlinNoise( 50, 50, 3, 0, true, true, 7 );//
			
			bmp = new Bitmap( bd );
			addChild( bmp );
			
			
			//copie le contenu texte dans le bitmapData
			var bdMask:BitmapData = new BitmapData( txt.width, txt.height, true, 0 );
			bdMask.draw( txt );
			
			
			//1-copie du canal alpha
			/*
				bd.copyChannel( bdMask, bdMask.rect, new Point(), BitmapDataChannel.ALPHA, BitmapDataChannel.ALPHA );
			//*/
			
			//2-création d'un masque 
			/*
				var masque:Bitmap = new Bitmap( bdMask );
				addChild( masque );
				bmp.cacheAsBitmap = masque.cacheAsBitmap = true;
				bmp.mask = masque;
			//*/
		}
		
	}
	
}